#ifdef PM_DEBUG

#include "common.h"
#include "system/pm_str.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/bmp_cursor.h"
#include "fld_bmp.h"
#include "fieldmap.h"
#include "field_common.h"
#include "system/fontproc.h"

#include "debug_saito.h"
#include "d_saito_st.h"

#include "system/brightness.h"

#include "camera_move_test.h"

#include "fieldsys.h"

#include "field_event.h"
#include "mapdefine.h"
#include "worldmap.h"
#include "player.h"
#include "zonedata.h"

#include "eventdata.h"		//ゾーン毎のイベントデータ参照のため

//↓ポケモン関連
#include "system/buflen.h"

#include "system/msgdata_util.h"

#include "msgdata/msg_debug_saito.h"
#include "msgdata/msg.naix"

#include "encount_set.h"

#include "savedata/encount.h"
#include "move_pokemon_def.h"

#include "move_pokemon.h"
#include "field/field_place_name.h"
#include "system/snd_tool.h"

#include "generate_zone.h"

#include "encount_define.h"
#include "arc/encdata_ex.naix"
#include "system/zukanwork.h"

#include "pair_poke.h"

#include "debug_saito.h"

#include "ev_pokemon.h"	//for EvPoke_GetLivingPokemonTop
#include "system/pm_overlay.h"
FS_EXTERN_OVERLAY( debug_pair_poke );

#define	FDBG_DEBUG_PL		(FLD_SYSFONT_PAL)	//パレットナンバー

#define DEBUG_LEAF_LOOP_NUM	(5)

#define HEAPID_DPP	(HEAPID_BASE_APP)


//----------------------------------------------------------------------------
//	構造体定義
//----------------------------------------------------------------------------
typedef struct DPP_WORK_tag
{
	struct _FIELDSYS_WORK * fsys;
	GF_BGL_BMPWIN	win;
	BMPCURSOR * cursor;
	int CurPoint;
	int HeapID;

	OVERLAY_CALL_BACK callback;
	POKEMON_PARAM* param;	//操作対象ポケモンパラム
	int Humor;			//機嫌値テンポラリ
	u16 LeafMonsNo;		//葉っぱ取得状況を調べるポケモンナンバー
	u8 Leaf[DEBUG_LEAF_LOOP_NUM];			//葉っぱ取得状況
	u8 dummy;


}DPP_WORK;

//前方宣言
void DPP_MakeBmpList(struct _FIELDSYS_WORK *fsys, OVERLAY_CALL_BACK callback);

static void SetupLeafDebugWindow(TCB_PTR tcb,void *work);
static void SetupHumorDebugWindow(TCB_PTR tcb,void *work);

static void SetLeafDebugWindow(struct _FIELDSYS_WORK *fsys, DEBUG_WINDOW_DATA *wp);
static void LeafDispTask(TCB_PTR tcb,void* work);
static void DrawLeaf(DPP_WORK *wk);

static void SetHumorDebugWindow(struct _FIELDSYS_WORK *fsys, DEBUG_WINDOW_DATA *wp);
static void HumorDispTask(TCB_PTR tcb,void* work);
static void DrawHumor(DPP_WORK *wk);

static const ID_PARAM DebugPPMenuList[] = {
	{ D_SAITO_LEAF, (u32)SetupLeafDebugWindow },
	{ D_SAITO_HUMOR, (u32)SetupHumorDebugWindow },
};

//ポケスロンメニュー
static const BMPWIN_DAT	DPP_ListWindowData = {
	GF_BGL_FRAME3_M,
	0,1,
	16,6,
	FDBG_DEBUG_PL,8
};

static const int leaf[DEBUG_LEAF_LOOP_NUM] = {
			ID_PARA_leaf0,ID_PARA_leaf1,ID_PARA_leaf2,
			ID_PARA_leaf3,ID_PARA_leaf4
		};

void DPP_MakeBmpList(struct _FIELDSYS_WORK *fsys, OVERLAY_CALL_BACK callback)
{
	//ウィンドウ構築
	MakeBmpList(fsys, NELEMS(DebugPPMenuList), DebugPPMenuList, &DPP_ListWindowData, callback);
}

static void SetupLeafDebugWindow(TCB_PTR tcb,void *work)
{
	struct _FIELDSYS_WORK * fsys;
	DEBUG_WINDOW_DATA *wp;
	wp = (DEBUG_WINDOW_DATA *)work;
	fsys = wp->fsys;

	//メインウィンドウを消す
	FreeDebugWindow(tcb,work);
	//メインウィンドウタスク消去
	TCB_Delete(tcb);
	//ウィンドウ構築
	SetLeafDebugWindow(fsys, wp);
}

static void SetupHumorDebugWindow(TCB_PTR tcb,void *work)
{
	struct _FIELDSYS_WORK * fsys;
	DEBUG_WINDOW_DATA *wp;
	wp = (DEBUG_WINDOW_DATA *)work;
	fsys = wp->fsys;

	//メインウィンドウを消す
	FreeDebugWindow(tcb,work);
	//メインウィンドウタスク消去
	TCB_Delete(tcb);
	//ウィンドウ構築
	SetHumorDebugWindow(fsys, wp);
}


static void SetLeafDebugWindow(struct _FIELDSYS_WORK *fsys, DEBUG_WINDOW_DATA *wp)
{
	DPP_WORK *wk = NULL;

	TCB_PTR	pTcb = NULL;
	wk = sys_AllocMemory(HEAPID_DPP,sizeof(DPP_WORK));
	wk->HeapID = HEAPID_DPP;
	wk->fsys = fsys;
	wk->cursor = BMPCURSOR_Create( HEAPID_DPP );
	wk->CurPoint = 0;
	wk->callback = wp->OverlayCallBack;

	//バトルできる先頭ポケモンの葉っぱ取得状況を取得
	{
		u8 i;
		SAVEDATA * save = GameSystem_GetSaveData(fsys);
		MYSTATUS * my =SaveData_GetMyStatus( save );
		POKEPARTY * party = SaveData_GetTemotiPokemon( save );
		wk->param = EvPoke_GetLivingPokemonTop(party);
		for (i=0;i<DEBUG_LEAF_LOOP_NUM;i++){
			wk->Leaf[i] = PokeParaGet(wk->param, leaf[i], NULL);
		}
		wk->LeafMonsNo = PokeParaGet(wk->param,ID_PARA_monsno,NULL);
	}

	//メイン処理タスク追加
	pTcb = TCB_Add(LeafDispTask, wk, 0);

	GF_BGL_BmpWinAdd( wk->fsys->bgl,&wk->win,GF_BGL_FRAME3_M, 0, 0, 5, 12, 1, 1 );

	SystemFontPaletteLoad( PALTYPE_MAIN_BG, 16<<1, wk->HeapID );
	//葉っぱ表示
	DrawLeaf(wk);
}

static void SetHumorDebugWindow(struct _FIELDSYS_WORK *fsys, DEBUG_WINDOW_DATA *wp)
{
	DPP_WORK *wk = NULL;

	TCB_PTR	pTcb = NULL;
	wk = sys_AllocMemory(HEAPID_DPP,sizeof(DPP_WORK));
	wk->HeapID = HEAPID_DPP;
	wk->fsys = fsys;
	wk->callback = wp->OverlayCallBack;

	//バトルできる先頭ポケモンの機嫌値を取得
	{
		SAVEDATA * save = GameSystem_GetSaveData(fsys);
		MYSTATUS * my =SaveData_GetMyStatus( save );
		POKEPARTY * party = SaveData_GetTemotiPokemon( save );
		wk->param = EvPoke_GetLivingPokemonTop(party);
		wk->Humor = PokeParaGet(wk->param,ID_PARA_humor,NULL);
	}
	//メイン処理タスク追加
	pTcb = TCB_Add(HumorDispTask, wk, 0);

	GF_BGL_BmpWinAdd( wk->fsys->bgl,&wk->win,GF_BGL_FRAME3_M, 0, 0, 6, 2, 1, 1 );

	SystemFontPaletteLoad( PALTYPE_MAIN_BG, 16<<1, wk->HeapID );
	//現在機嫌値表示
	DrawHumor(wk);
}

//機嫌値ウィンドウ表示中タスク
static void HumorDispTask(TCB_PTR tcb,void* work)
{
	u8 evt_no;
	BOOL end;
	BOOL redraw;
	DPP_WORK *wk = (DPP_WORK *)work;

	end = FALSE;
	redraw = FALSE;

	if (sys.trg & PAD_KEY_UP){
		wk->Humor+=10;
		redraw = TRUE;
	}else if(sys.trg & PAD_KEY_DOWN){
		wk->Humor-=10;
		redraw = TRUE;
	}else if(sys.trg & PAD_KEY_LEFT){
		wk->Humor--;
		redraw = TRUE;
	}else if(sys.trg & PAD_KEY_RIGHT){
		wk->Humor++;
		redraw = TRUE;
	}else if ( (sys.trg & PAD_BUTTON_A)||(sys.trg & PAD_BUTTON_B) ){
		//機嫌値反映
		PokeParaPut(wk->param, ID_PARA_humor, &wk->Humor);
		end = TRUE;
	}

	if (redraw){
		//境界チェック
		if (wk->Humor < -127){
			wk->Humor = -127;
		}else if(wk->Humor > 127){
			wk->Humor = 127;
		}
		DrawHumor(wk);
	}

	if (end){
		struct _FIELDSYS_WORK * fsys;
		OVERLAY_CALL_BACK callback = wk->callback;
		fsys = wk->fsys;

		GF_BGL_BmpWinOff( &wk->win );
		GF_BGL_BmpWinDel( &wk->win );

		sys_FreeMemoryEz((void *)work);
		//タスク消去
		TCB_Delete(tcb);

		//ウィンドウ構築
		MakeBmpList(fsys, NELEMS(DebugPPMenuList), DebugPPMenuList, &DPP_ListWindowData, callback);
	}
}

//葉っぱウィンドウ表示中タスク
static void LeafDispTask(TCB_PTR tcb,void* work)
{
	u8 evt_no;
	BOOL end;
	BOOL redraw;
	DPP_WORK *wk = (DPP_WORK *)work;

	end = FALSE;
	redraw = FALSE;

	if (sys.trg & PAD_KEY_UP){
		if (wk->CurPoint == 0){
			wk->CurPoint = DEBUG_LEAF_LOOP_NUM-1;
		}else{
			wk->CurPoint--;
		}
		redraw = TRUE;
	}else if(sys.trg & PAD_KEY_DOWN){
		if (wk->CurPoint == DEBUG_LEAF_LOOP_NUM-1){
			wk->CurPoint = 0;
		}else{
			wk->CurPoint++;
		}
		redraw = TRUE;
	}else if ( sys.trg & PAD_BUTTON_A ){
		//葉っぱセット
		wk->Leaf[wk->CurPoint] = 1;
		redraw = TRUE;
	}else if ( sys.trg & PAD_BUTTON_B ){
		u8 i;
		//データ反映
		for (i=0;i<DEBUG_LEAF_LOOP_NUM;i++){
			PokeParaPut(wk->param, leaf[i], &wk->Leaf[i]);
		}
		end = TRUE;
	}


	if (redraw){
		DrawLeaf(wk);
	}

	if (end){
		struct _FIELDSYS_WORK * fsys;
		OVERLAY_CALL_BACK callback = wk->callback;
		fsys = wk->fsys;

		//カーソル削除
		BMPCURSOR_Delete( wk->cursor );

		GF_BGL_BmpWinOff( &wk->win );
		GF_BGL_BmpWinDel( &wk->win );

		sys_FreeMemoryEz((void *)work);
		//タスク消去
		TCB_Delete(tcb);

		//ウィンドウ構築
		MakeBmpList(fsys, NELEMS(DebugPPMenuList), DebugPPMenuList, &DPP_ListWindowData, callback);
	}
}

//葉っぱ表示
static void DrawLeaf(DPP_WORK *wk)
{
	int i;
	STRBUF *msg_buf;
	MSGDATA_MANAGER* man;

	GF_BGL_BmpWinDataFill( &wk->win, 0x0f );

	BMPCURSOR_Print( wk->cursor, &wk->win, 0, 16+wk->CurPoint*16 );

	man = MSGMAN_Create(MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_debug_saito_dat, wk->HeapID);
	msg_buf = STRBUF_Create(16,wk->HeapID);

	STRBUF_SetNumber( msg_buf, wk->LeafMonsNo, 3, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
	GF_STR_PrintSimple(&wk->win, FONT_SYSTEM, msg_buf, 1*8, 0, MSG_ALLPUT, NULL );

	for (i=0;i<DEBUG_LEAF_LOOP_NUM;i++){
		int msg;
		if (wk->Leaf[i]){
			msg = D_SAITO_LEAF_OK;
		}else{
			msg = D_SAITO_LEAF_NG;
		}
		MSGMAN_GetString(man, msg ,msg_buf);
		GF_STR_PrintSimple(&wk->win, FONT_SYSTEM, msg_buf, 1*8, 16+i*16, MSG_ALLPUT, NULL );
	}

	STRBUF_Delete( msg_buf );
	MSGMAN_Delete( man );

	GF_BGL_BmpWinOn( &wk->win );
}

//機嫌値表示
static void DrawHumor(DPP_WORK *wk)
{
	int i;
	STRBUF *msg_buf;
	MSGDATA_MANAGER* man;

	GF_BGL_BmpWinDataFill( &wk->win, 0x0f );

	man = MSGMAN_Create(MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_debug_saito_dat, wk->HeapID);
	msg_buf = STRBUF_Create(16,wk->HeapID);

	STRBUF_SetNumber( msg_buf, wk->Humor, 3, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
	GF_STR_PrintSimple(&wk->win, FONT_SYSTEM, msg_buf, 1*8, 0, MSG_ALLPUT, NULL );

	STRBUF_Delete( msg_buf );
	MSGMAN_Delete( man );

	GF_BGL_BmpWinOn( &wk->win );
}



#endif	//PM_DEBUG

